package se.webbzon.oschi01.worldcamera;

import java.awt.Dimension;

import javax.media.j3d.J3DGraphics2D;

import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldarea.WorldAppearance3D;

import com.sun.j3d.utils.universe.SimpleUniverse;

@Friendly class SimpleAWTWorldView extends AbstractWorldView implements SimpleWorldView {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = 4042431742765546042L;

	// The simple universe of this view
	private final SimpleUniverse u;
	
	// The camera components of this world view
	private final WorldCamera camera;
	private final WorldCameraHud hud;
	private final WorldCameraBackground background;
	
	// The 2D-graphics objects used in preRender and postRender
    private final J3DGraphics2D g3d;
    
    // The world view symbiosis of this world view
    private WorldViewSymbiosis symb;

	/*============================================================
	Constructors
	============================================================*/

    @Friendly SimpleAWTWorldView(int viewProperties, Dimension d) {
		super(SimpleUniverse.getPreferredConfiguration());
		
		u = new SimpleUniverse(this);
		
		if (isSet(viewProperties,TRANSPARENCY))
            camera = new SimpleWorldCamera(WorldCamera.TRANSPARENCY, u.getViewingPlatform());
		else
            camera = new SimpleWorldCamera(0, u.getViewingPlatform());
		hud = new WorldCameraHud(this,d);
		background = new WorldCameraBackground(this,d,false);
		
		// Create the 3D canvas
        setPreferredSize(new Dimension((int) d.getWidth(),(int) d.getHeight()));
        g3d = getGraphics2D();
	}
	
	/*============================================================
	Constructors
	============================================================*/
	
    @Override public SimpleUniverse getUniverse() {
		return u;
	}

	@Override public void preRender(WorldAppearance3D world) {
		world.performPreDraw(background);
	}

	@Override public void postRender(WorldAppearance3D world) {
		world.performPostDraw(hud);
	}

	@Override public boolean setSymbiosis(WorldViewSymbiosis symb) {
		this.symb = symb;
		return true;
	}

	@Override public WorldCamera getCamera() {
		return camera;
	}

	@Override public WorldCameraPane getCameraHud() {
		return hud;
	}

	@Override public WorldCameraPane getCameraBackground() {
		return background;
	}
	
	@Override public void preRender() {
		background.clear();
		if (symb != null)
			symb.preRender(this);
		background.paint(g3d, this);
	}

	@Override public void postRender() {
		super.postRender();
		hud.clear();
		if (symb != null)
			symb.postRender(this);
		hud.paint(g3d, this);
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Returns true if the given property is set in the given properties. **/
	private static final boolean isSet(int properties, int property) {
		return ((properties & property) == property);
	}

}
